package dao

import (
	"activity_srv/activity"
	"activity_srv/model"
	"errors"
	"fmt"
	"go.uber.org/zap"
	"smart-health-platforms/pkg"
	"time"
)

// 拼团商品发布
func GrouponProductAdd(in *activity.CreateGrouponProductRequest) (*model.GrouponProduct, error) {

	//接收参数
	activityStart, err := time.Parse(time.DateTime, in.ActivityStart)
	if err != nil {
		return nil, errors.New("时间格式错误")
	}
	activityEnd, err := time.Parse(time.DateTime, in.ActivityEnd)
	if err != nil {
		return nil, errors.New("时间格式错误")
	}
	//ActivityStart := time.Now()
	product := &model.GrouponProduct{
		ProductStock: uint(in.ProductStock),
		ProductName:  in.ProductName,
		ProductPrice: float64(in.ProductPrice),
		GroupPrice:   float64(in.GroupPrice),
		MemberNum:    uint(int(in.MemberNum)),
		/*Status:        uint(in.Status),*/
		ActivityStart: activityStart,
		ActivityEnd:   activityEnd,
		CoverImage:    in.CoverImage,
	}
	fmt.Println(product)
	pkg.Info("拼团商品发布成功",
		zap.Int("id", int(product.Id)),
		zap.String("ProductName", product.ProductName),
	)
	err = product.CreateGrouponProduct()

	if err != nil {
		pkg.Warn("拼团商品发布失败")

		return nil, errors.New("拼团商品发布失败")
	}
	return product, err
}

// 拼团商品列表展示
func GrouponProductList(in *activity.GetGrouponProductListRequest) ([]*activity.GrouponProductList, error) {
	var pro model.GrouponProduct
	list, err := pro.GetGrouponProductList(in.ProductName)
	if err != nil {
		return nil, errors.New("拼团商品列表展示失败")
	}
	var productList []*activity.GrouponProductList
	for _, product := range list {
		lists := activity.GrouponProductList{
			ProductName:  product.ProductName,
			ProductPrice: float32(product.ProductPrice),
			GroupPrice:   float32(product.GroupPrice),
			MemberNum:    int64(product.MemberNum),
			Status:       int64(product.Status),
			CoverImage:   product.CoverImage,
		}
		productList = append(productList, &lists)
	}
	return productList, err
}

// 获取拼团商品详情
func GetGrouponProductDetail(in *activity.GetGrouponProductDetailRequest) (*model.GrouponProduct, error) {
	var product model.GrouponProduct
	if err := product.GetGrouponProductById(int(in.Id)); err != nil {
		return nil, err
	}
	if product.Id == 0 {
		return nil, errors.New("该拼团商品不存在，请重试")
	}
	err := product.GetGrouponProductDetail(int(in.Id))
	pkg.Info("获取拼团商品详情成功",
		zap.Int("id", int(product.Id)),
		zap.String("ProductName", product.ProductName),
	)
	if err != nil {
		pkg.Warn("获取拼团商品详情失败")
		return nil, errors.New("获取拼团商品详情失败")
	}
	return &product, err
}

// 修改拼团商品信息
func UpdateGrouponProduct(in *activity.UpdateGrouponProductRequest) (*model.GrouponProduct, error) {
	var product model.GrouponProduct
	if err := product.GetGrouponProductById(int(in.Id)); err != nil {
		return nil, err
	}

	if product.ProductName == "" {
		return nil, errors.New("该拼团商品不存在，禁止修改商品信息")
	}
	activityStart, err := time.Parse(time.DateTime, in.ActivityStart)
	if err != nil {
		return nil, errors.New("时间格式错误")
	}
	activityEnd, err := time.Parse(time.DateTime, in.ActivityEnd)
	if err != nil {
		return nil, errors.New("时间格式错误")
	}
	product.ProductName = in.ProductName
	product.ProductPrice = float64(in.ProductPrice)
	product.GroupPrice = float64(in.GroupPrice)
	product.MemberNum = uint(in.MemberNum)
	product.Status = uint(in.Status)
	product.ActivityStart = activityStart
	product.ActivityEnd = activityEnd
	product.CoverImage = in.CoverImage
	product.ProductStock = uint(in.ProductStock)
	err = product.UpdateGrouponProduct(int(in.Id))
	pkg.Info("修改拼团商品信息成功",
		zap.Int("id", int(product.Id)),
		zap.String("ProductName", product.ProductName),
	)
	if err != nil {
		pkg.Warn("修改拼团商品信息失败")
		return nil, errors.New("修改拼团商品失败")
	}
	return &product, nil
}

// 删除拼团商品信息
func DeleteGrouponProduct(in *activity.DeleteGrouponProductRequest) (*model.GrouponProduct, error) {
	var product model.GrouponProduct
	if err := product.GetGrouponProductById(int(in.Id)); err != nil {
		return nil, err
	}

	if product.ProductName == "" {
		return nil, errors.New("该拼团商品不存在，禁止修改商品信息")
	}
	err := product.DeleteGrouponProduct(int(in.Id))
	pkg.Info("删除拼团商品信息成功",
		zap.Int("id", int(product.Id)),
	)
	if err != nil {
		pkg.Warn("删除拼团商品信息失败")
		return nil, errors.New("删除拼团商品新消息失败")
	}
	return &product, err
}
